package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags = "套餐管理相关接口")
@RequestMapping("/admin/setmeal")
@RestController("adminSetmealController")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO 套餐数据
     * @return 响应成功结果
     */
    @ApiOperation(value = "新增套餐")
    @PostMapping
    @Transactional
    @CacheEvict(value = "setmealList", allEntries = true) // 清除 setmealList 所有缓存
    public Result<Object> addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids 套餐id集合
     */
    @ApiOperation(value = "删除套餐")
    @ApiImplicitParam(name = "ids", value = "套餐id集合", required = true, paramType = "query", dataType = "long")
    @DeleteMapping
    @Transactional
    @CacheEvict(value = "setmealList", allEntries = true) // 清除 setmealList 所有缓存
    public Result<Object> deleteSelemeal(@RequestParam List<Long> ids) {
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id 套餐id
     * @return 套餐详情
     */
    @ApiOperation(value = "根据id获取套餐详情")
    @ApiImplicitParam(name = "id", value = "套餐id", required = true, paramType = "path", dataType = "long")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    /**
     * 更新套餐
     * @param setmealDTO 套餐数据
     */
    @ApiOperation(value = "更新套餐")
    @PutMapping
    @Transactional
    @CacheEvict(value = "setmealList", allEntries = true) // 清除 setmealList 所有缓存
    public Result<Object> updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 更新套餐状态
     * @param status 套餐状态
     * @param id 套餐id
     */
    @ApiOperation(value = "更新套餐状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "套餐状态（0:停用 1:启用）", required = true, paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "id", value = "套餐id", required = true, paramType = "query", dataType = "long")
    })
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealList", allEntries = true) // 清除 setmealList 所有缓存
    public Result<Object> updateSetmealStatus(@PathVariable Integer status, @RequestParam Long id) {
        setmealService.updateSetmealStatus(status, id);
        return Result.success();
    }

    /**
     * 分页查询套餐列表
     * @param setmealPageQueryDTO 分页查询参数
     * @return 套餐列表
     */
    @ApiOperation(value = "分页查询套餐列表")
    @GetMapping("/page")
    public Result<PageResult> getSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.getSetmealPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
}
